package com.cfh.web.xml;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.cfh.ahead.Constant;
import com.cfh.service.XmlService;
import com.opensymphony.xwork2.ActionSupport;

public class CustRegXml extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(CustRegXml.class);

	@SuppressWarnings("unchecked")
	private Map session;

	@SuppressWarnings("unchecked")
	public void setSession(Map map) {
		session = map;
	}

	private XmlService xmlService = null;
	private String xml;
	private String answer;
	private String step1;
	private String step2;
	private String step3;
	private String step4;

	private String image;
	private String name;
	private String city;
	private String mobile;
	private String email;
	private String prcid;
	private String agreeflg;
	private String company;
	private String from;
	private String town;

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPrcid() {
		return prcid;
	}

	public void setPrcid(String prcid) {
		this.prcid = prcid;
	}

	public String getAgreeflg() {
		return agreeflg;
	}

	public void setAgreeflg(String agreeflg) {
		this.agreeflg = agreeflg;
	}

	public String getStep1() {
		return step1;
	}

	public void setStep1(String step1) {
		this.step1 = step1;
	}

	public String getStep2() {
		return step2;
	}

	public void setStep2(String step2) {
		this.step2 = step2;
	}

	public String getStep3() {
		return step3;
	}

	public void setStep3(String step3) {
		this.step3 = step3;
	}

	public String getStep4() {
		return step4;
	}

	public void setStep4(String step4) {
		this.step4 = step4;
	}

	@SuppressWarnings("unchecked")
	public String execute() {
		String imageSession = null;
		imageSession = (String) session.get(Constant.CAPTCHA_SESSION_HOLDER);
		try {
			logger.debug("CustRegXml,step1=" + step1 + ",step2=" + step2 + ",step3=" + step3 + ",step4=" + step4);
			StringBuffer sb = new StringBuffer();
			for (int i = 1; i <= 4; i++) {
				String stepX = null;
				if (i == 1) {
					stepX = step1;
				}
				if (i == 2) {
					stepX = step2;
				}
				if (i == 3) {
					stepX = step3;
				}
				if (i == 4) {
					stepX = step4;
				}

				if (stepX != null) {
					String[] tmpArray = stepX.split(",");
					for (int j = 0; j < tmpArray.length; j++) {
						String tmpStr = tmpArray[j];
						if (tmpStr != null && tmpStr.trim().length() > 0) {
							sb.append("Q" + i + "_" + addOne(tmpStr) + "^");
						}

					}
				}

			}

			answer = sb.toString();
			logger.debug("CustRegXml,answer=" + answer);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("error in action", e);
			throw new RuntimeException(e);
		}

		try {
			String[] answers = answer.split("\\^");
			HttpServletRequest req = ServletActionContext.getRequest();
			String add = req.getRemoteAddr();
			xml = getXmlService().custreg(answers, add, imageSession, image, name, city, mobile, email, prcid, agreeflg, company, from, town);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("error in action", e);
			throw new RuntimeException(e);
		}

		{// init CAPTCHA_SESSION_HOLDER if SUCCESS
			if (xml.indexOf("<returncode>00</returncode>") > 0) {
				session.put(Constant.CAPTCHA_SESSION_HOLDER, Constant.CAPTCHA_SESSION_INIT_VALUE);
			}
		}
		return SUCCESS;
	}

	private String addOne(String tmpStr) {
		int newInt = 1;
		try {
			int oldInt = Integer.parseInt(tmpStr);
			newInt = oldInt + 1;
		} catch (Exception printAndIgnore) {
			printAndIgnore.printStackTrace();
			logger.error("error in action", printAndIgnore);
			newInt = 1;
		}

		return String.valueOf(newInt);
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public String getXml() {
		return xml;
	}

	public void setXmlService(XmlService xmlService) {
		this.xmlService = xmlService;
	}

	public XmlService getXmlService() {
		return xmlService;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getCompany() {
		return company;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getFrom() {
		return from;
	}

	public void setTown(String town) {
		this.town = town;
	}

	public String getTown() {
		return town;
	}

}
